package Model;

import java.sql.ResultSet;
import java.sql.Statement;

public class Car {
	   private DbConnection db = null;
	   private Statement st = null;
	   private ResultSet rs = null; 
	   
	   private int id_car=-1;
	   private int max_speed=-1;
	   private String name=null;
	   private String model=null;
	   private String mark=null;
	   private int doors=-1;
	   private int engine=-1;
	   
	   
	   public Car(){
		   db = new DbConnection();	   
		   st = db.getStatement();
	   }
	   
	   public Car(int id_car){
		   db = new DbConnection();	   
		   st = db.getStatement();
		   
		   try{
				this.rs = this.st.executeQuery("Select * from car "+
												" WHERE id_car='"+id_car+"';");
				this.rs.next();
				this.id_car = this.rs.getInt("id_car");
				this.max_speed = this.rs.getInt("max_speed");
				this.name= this.rs.getString("name");
				this.model = this.rs.getString("model");
				this.mark = this.rs.getString("mark");
				this.doors = this.rs.getInt("doors");
				this.engine = this.rs.getInt("engine");
				
				
			}catch (Exception e) {
				System.out.print(e.toString());
			}   
	   }
	   
	   public int getIdCar(){
		   return this.id_car;
	   }
	   
	   public int getMaxSpeed(){
		   return this.max_speed;
	   }
	   
	   public String getName(){
		   return this.name;
	   }
	   
	   public String getModel(){
		   return this.model;
	   }
	   
	   public String getMark(){
		   return this.mark;
	   }
	   
	   public int getDoors(){
		   return this.doors;
	   }
	   
	   public int getEngine(){
		   return this.engine;
	   }
	   
	   
	   public void setCar(int id_car){
		   db = new DbConnection();	   
		   st = db.getStatement();
		   
		   try{
				this.rs = this.st.executeQuery("Select * from car "+
												" WHERE id_car='"+id_car+"';");
				this.rs.next();
				this.id_car = this.rs.getInt("id_car");
				this.max_speed = this.rs.getInt("max_speed");
				this.name= this.rs.getString("name");
				this.model = this.rs.getString("model");
				this.mark = this.rs.getString("mark");
				this.doors = this.rs.getInt("doors");
				this.engine = this.rs.getInt("engine");
				
				
			}catch (Exception e) {
				System.out.print(e.toString());
			}   
	   }
	   
	   
	   
	   
	   //METODY BAZODANOWE

	   public int insertCar(int  max_speed,String name,String  model,String mark, int doors,int engine){
		   int result=-1;
		   try{
				this.rs = this.st.executeQuery("INSERT INTO car( "+
								            "  max_speed, \"name\", model, mark, doors, engine) "+
								            " VALUES ("+max_speed+", '"+name+"', '"+model+"', '"+mark+"', "+doors+", "+engine+") returning id_car;");
				
				this.rs.next();
				result = this.rs.getInt("id_car");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
		}
	   
	   public int deleteCarById(int id){
		   
		   int result = -1;
			try{
				this.rs = this.st.executeQuery("DELETE FROM car WHERE id_car="+id+" returning id_car");
				
				this.rs.next();
				result = this.rs.getInt("id_car");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
		   
	   }
	   
	   public int updateCar(int  max_speed,String name,String  model,String mark, int doors,int engine , int id_car){
		   int result=-1;
		   try{
				this.rs = this.st.executeQuery("UPDATE car "+
							   " SET max_speed="+max_speed+", \"name\"='"+name+"', model='"+model+"', mark='"+mark+"', doors="+doors+", engine="+engine+" "+
							 " WHERE id_car="+id_car+" returning id_car;");
				
				this.rs.next();
				result = this.rs.getInt("id_car");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
	   }
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
}
